package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.model.Board;
import com.example.forum.service.imp.BoardServiceImp;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/board")
public class BoardController {
    @Autowired
    private BoardServiceImp boardServiceImp;
    @Value("${bit-forum.index.board-num:9}")
    private Integer indexBoard;
    /**
     * 展示页面导航栏
     * @return
     */
    @RequestMapping(value = "/topList",method = RequestMethod.GET)
    public AppResult<List<Board>> topList(){
        List<Board> boards = boardServiceImp.selectByNum(indexBoard);

        //如果这里的board为空就返回一个空的列表
        if(boards == null){
            boards = new ArrayList<>();
        }
        return AppResult.success(boards);
    }

    /**
     * 获取板块信息
     * @param id
     * @return
     */
   @RequestMapping(value = "/getById" ,method = RequestMethod.GET)
    public AppResult<Board> getBoardInfo (@RequestParam(value = "id") @NonNull Long id) {
       // 查询
       Board result = boardServiceImp.selectById(id);

       //对象的校验
       if (result == null || result.getDeleteState() == 1) {
           AppResult.fail(ResultCode.FAILED_BOARD_NOT_EXISTS);
       }
       // 返回结果
       return AppResult.success(result);
   }
}
